package com.apes.fn.scm.used.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.product.model.Brand;
import com.apes.scm.masterdata.product.model.Classify;
import com.apes.scm.masterdata.product.model.ServeType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/***
 * 旧件（不包含赠品）
 * 1. 旧件商品改为手工输入，即为基本计量单位，入库单位
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Used.CONST_TABLE_NAME)
public class Used extends Model {
    public static final String CONST_TABLE_NAME = "war_used";

    @Id
    @Column(name = "id")
    @Comment("旧件编码")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "旧件", padding = 8)
    private String id;

    @Name
    @Comment("旧件名称")
    @Column(name = "name", length = 200)
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brand_id", referencedColumnName = "id")
    @Comment("品牌")
    private Brand brand;  //品牌

    @Column(name = "specifications", length = 200)
    @Comment("规格")
    private String specifications; // 规格

    @Column(name = "model", length = 200)
    @Comment("型号")
    private String model;  //型号


    // 旧件单位只做展示不做关联
    @Column(name = "uom", length = 64)
    @Comment("计量单位")
    private String uom;  //计量单位


    @Column(name = "colour", length = 100)
    @Comment("颜色")
    private String colour;  //颜色

    @Column(name = "DESCRIPTION", length = 500)
    @Comment("描述")
    private String description; //描述

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效标记")
    private boolean valid = true; //有效

    @Transient
    private double stockQty; // 标准库存数量（临时展示）

    @Transient
    private double insuranceStockQty; // 保险库存数量（临时展示）

    /***
     * 功能： 旧件分类明细
     */
    @OneToMany(mappedBy = "used", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<UsedClassify> usedClassifyItems = new ArrayList<>();

    public List<UsedClassify> getUsedClassifyItems() {
        if (usedClassifyItems == null) return null;
        for (UsedClassify item : usedClassifyItems) {
            item.setUsed(this);
        }
        return usedClassifyItems;
    }



//    // 拼接商品分类用于查看
//    @Transient
//    private String classify;
//
//    public String getClassify() {
//        if( usedClassifyItems.size() == 0 ) return "";
//        return usedClassifyItems.stream().reduce("", (d1, d2) -> {
//            if( d1.equals("") ) return d2.getClassifyName();
//            return d1 + ";" + d2.getClassifyName();
//        },String::concat);
//    }

}